//
//  InvoiceStatisticListVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/27.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "InvoiceStatisticListVC.h"
#import "InvoiceStatisticListCell.h"
#import "InvoiceStatisticListModel.h"
#import "InvoiceStatisticsDetailVC.h"

@interface InvoiceStatisticListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) InvoiceStatisticListCell *invoiceStatisticListCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (copy, nonatomic) NSString *strStatus; // 货源状态
//@property (nonatomic,copy) NSString *strIsOftenOrder;  // 0不是 1是
//@property (nonatomic,copy) NSString *strIsCancel; // 0不是 1是
@end

@implementation InvoiceStatisticListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor cjBackgroundColor];
    DLog(@"======%@",self.indexPage);

    self.invoiceStatisticListCell = [[InvoiceStatisticListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

/// <#Description#>
- (void)loadData {
//    @[@"全部",@"未接单",@"已接单",@"已装货",@"已卸货",@"已核算",@"已完成""]

    if (self.indexPage.intValue == 0) {
        self.strStatus = @"";

    }else if (self.indexPage.intValue == 1){
     
        self.strStatus = @"0";

    }else if (self.indexPage.intValue == 2){
     
        self.strStatus = @"1";

    }else if (self.indexPage.intValue == 3){
   
        self.strStatus = @"2";

    }
//    else if (self.indexPage.intValue == 4){
//      
//        self.strStatus = @"3";
//
//    }else if (self.indexPage.intValue == 5){
//
//        self.strStatus = @"4";
//
//    }else if (self.indexPage.intValue == 6){
//     
//        self.strStatus = @"5";
//
//    }
    
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/invoice",CJBaseUrl);
        NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                                 @"pageSize":@"10",
                                 @"invoiceStatus":self.strStatus,
                                };
        WS(ws);
        [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {

            DLog(@"++++++++%@",responseObject);

            NSArray *list = responseObject[@"records"];
            NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
            if (list.count > 0) {

                if ([ws.refreshType isEqualToString:@"down"]) {
                    [ws.modelArray removeAllObjects];
                }

                NSError * error ;
                NSArray *arr = [InvoiceStatisticListModel arrayOfModelsFromDictionaries:list error:&error];
                [ws.modelArray addObjectsFromArray:arr];

                [ws.tableView.mj_header endRefreshing];
                [ws.tableView.mj_footer endRefreshing];

                if (ws.modelArray.count == total.intValue) {
                    [ws.tableView.mj_footer endRefreshingWithNoMoreData];
                }

            }else{

                [ws.tableView.mj_header endRefreshing];
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [ws.tableView reloadData];

        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
        }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"InvoiceStatisticListCell";
    InvoiceStatisticListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[InvoiceStatisticListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    InvoiceStatisticListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//InvoiceStatisticsDetailVC
    InvoiceStatisticListModel *model = self.modelArray[indexPath.row];
    InvoiceStatisticsDetailVC *vc = [InvoiceStatisticsDetailVC new];
    vc.strCode = model.code;
    [self pushVc:vc];
}

- (void)pushVc:(UIViewController *)vc {
    
    if ([vc isKindOfClass:[UIViewController class]] == NO) return ;
    if (self.navigationController == nil) return ;
    if (vc.hidesBottomBarWhenPushed == NO) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    
    return _tableView;
}

@end
